package com.cskaoyan.controller.wx;

import com.cskaoyan.bean.BasePageData;
import com.cskaoyan.bean.BaseParam;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.OrderGoods;
import com.cskaoyan.bean.wx.vo.*;
import com.cskaoyan.service.OrderService;
import com.cskaoyan.utils.ErrState;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author Jcy
 * 订单
 */
@RestController
@RequestMapping("wx/order")
public class OrderWxController {

    @Autowired
    OrderService orderService;

    @RequestMapping("list")
    public BaseRespVo list(BaseParam param, Short showType){
        BasePageData data = orderService.listWx(param,showType);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId){
        OrderDetailWxVO orderDetailWxVO = orderService.detailWx(orderId);
        return BaseRespVo.ok(orderDetailWxVO);
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map map){
        Integer orderId = ((Integer) map.get("orderId"));
        orderService.cancel(orderId);
        return BaseRespVo.ok();
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        orderService.refundWx(orderId);
        return BaseRespVo.ok();
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        orderService.confirm(orderId);
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map){
        Integer orderId = (Integer) map.get("orderId");
        orderService.deleteWx(orderId);
        return BaseRespVo.ok();
    }

    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId, Integer goodsId){
        OrderGoods orderGoods = orderService.goods(orderId,goodsId);
        return BaseRespVo.ok(orderGoods);
    }

    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody OrderCommentVO orderCommentVO){
        orderService.comment(orderCommentVO);
        return BaseRespVo.ok();
    }

    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody OrderPrepayVO orderPrepayVO){
        int code = orderService.prepay(orderPrepayVO.getOrderId());
        if (code == ErrState.GOODSNUM_NOT_ENOUGH.getErrno()){
            return BaseRespVo.fail(ErrState.GOODSNUM_NOT_ENOUGH.getErrmsg());
        }
        BaseRespVo baseRespVo = new BaseRespVo();
        baseRespVo.setErrno(ErrState.ORDER_PAY_FAIL.getErrno());
        baseRespVo.setErrmsg(ErrState.ORDER_PAY_FAIL.getErrmsg());
        return baseRespVo;
    }

    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody OrderSubmitInfoVO submitInfoVO){
        OrderSubmitVO orderSubmitVO = orderService.submit(submitInfoVO);
        return BaseRespVo.ok(orderSubmitVO);
    }
}
